/**
 * Created by ahwwl on 17-8-23.
 */
(function (original) {
    jQuery.fn.clone = function () {
        var result = original.apply(this, arguments),
            my_textareas = this.find('textarea').add(this.filter('textarea')),
            result_textareas = result.find('textarea').add(result.filter('textarea')),
            my_selects = this.find('select').add(this.filter('select')),
            result_selects = result.find('select').add(result.filter('select'));

        for (var i = 0, l = my_textareas.length; i < l; ++i) $(result_textareas[i]).val($(my_textareas[i]).val());
        for (var i = 0, l = my_selects.length; i < l; ++i)
        {
            $(result_selects[i]).val($(my_selects[i]).val()).trigger("change");
            // result_selects[i].selectedIndex = my_selects[i].selectedIndex;
        }
        return result;
    };
}) (jQuery.fn.clone);
(function($){
    var formSelecttextobj = $('<div class="form-beautify-unselect form-beautify-select" ><div class="form-beautify-select-title"><div class="form-beautify-text"></div> <i class="form-beautify-edge"></i></div></div>');

    $.fn.formSelect = function(){
        $(this).each(function (i) {
            selectSetValues($(this));
            // $(this).hide();
        });
    };
    $.fn.formCheckLabel = function () {
        $(this).each(function () {
            if(!$(this).parent().is("label"))
            {
                var  checkboxobj1= $("<label class='checkbox'></label>");

                $(this).after(checkboxobj1);
                $(this).prependTo(checkboxobj1);
                checkboxobj1.append("<span></span>");
                checkboxobj1.find("span").html($(this).attr("title"));

            }
        });
    };
    $.fn.addTab = function (s) {
        var _this = $(this);
        var default_s = {
            title: '',
            content:'',
            closeBtn: false,
            id:new Date().getTime()
        };
        s = $.extend({},default_s,s);
        var is_ext = false;
        $(".nav-tabs li[data-id]",_this).each(function () {
            var id = $(this).attr("data-id");
            if(id == s.id)
            {

                is_ext = true;
            }
        });
        if(is_ext)
        {
            _this.selectTab({id:s.id});
            return false;
        }
        var li = $('<li></li>');
        var lia = $('<a href="javascript:void 0;"></a>');
        var closeB = $('<span class="fa fa-times-circle close"></span>');
        var tab_content = $('<div class="tab-pane"></div>');
        li.attr("data-id",s.id);
        tab_content.attr("data-id",s.id);
        if(s.title)
        {
            lia.append(s.title);
        }
        closeB.appendTo(lia);
        lia.appendTo(li);
        if(!s.closeBtn)
        {
            li.addClass("noclose")
        }
        if($(".nav-tabs",_this).length>0)
        {
            li.appendTo($(".nav-tabs",_this));
        }
        if($(".nav-tabs",_this).height()>35)
        {
            $(".nav-tabs",_this).width($(".nav-tabs",_this).width()+li.width());
        }

        if(s.content)
        {
            tab_content.append(s.content);
        }

        if($(".tab-content",_this).length>0)
        {
            tab_content.appendTo($(".tab-content",_this));
        }
        _this.selectTab({id:s.id});
    };
    $.fn.selectTab = function (s) {
        var _this = $(this);

        if(s.id)
        {
            $(".nav-tabs>li.active,.tab-content>.tab-pane.active",_this).removeClass("active");
            $(".nav-tabs>li,.tab-content>.tab-pane",_this).each(function () {
                var id = $(this).attr("data-id");
                if(id == s.id)
                {
                    var w1 = $(".tab-header",_this).width();
                    var l1 = $(".tab-header",_this).offset().left+25;
                    var l2 = ($(this).offset().left+$(this).width()-l1-w1);
                    if(l2>0)
                    {
                        $(".nav-tabs",_this).offset({left:$(".nav-tabs",_this).offset().left-l2});
                    }


                    $(this).addClass("active");
                }
            });
        }
    };
    $.fn.closeTab = function (s) {
        var _this = $(this);
        if(s.id)
        {
            var id = "";
            $(".nav-tabs>li,.tab-content>.tab-pane",_this).each(function () {
                var ids = $(this).attr("data-id");
                if(ids == s.id)
                {

                    if($(this).next("li").length>0 &&$(this).hasClass("active"))
                    {
                       id = $(this).next("li").attr("data-id");

                    }else if($(this).prev("li").length>0&&$(this).hasClass("active")){
                        id = $(this).prev("li").attr("data-id");

                    }

                    $(this).remove();

                }
            });

            if(id)
            {

                _this.selectTab({id:id});
            }
        }
    };
    var selectCreate = function (selectobj) {
        var selecttextobj1 = formSelecttextobj.clone();
        var is_multiple = selectobj[0].multiple;
        var is_disabled = selectobj[0].disabled;
        if(selectobj.next().is(".form-beautify-select"))
        {
            selectobj.next().remove();
        }

        selecttextobj1.attr("data-multiple",is_multiple);

        selecttextobj1.find(".form-beautify-text").attr("placeholder",selectobj.attr("data-placeholder"));

        selectobj.after(selecttextobj1);
        if(is_disabled)
        {
            selecttextobj1.addClass("form-beautify-disabled");
        }
        var w = selectobj.css("width");
        if(w&&w!='0px')
        {
            selecttextobj1.css("width",w);
        }else{
            selecttextobj1.css("width","100%");
        }

        selectobj.hide();
    };

    var selectSetValues = function(selectobj)
    {
        if(!selectobj.next().is(".form-beautify-select"))
        {
            selectCreate(selectobj);
        }

        if(selectobj.is(":disabled"))
        {
            selectobj.next().addClass("form-beautify-disabled");
        }else{
            selectobj.next().removeClass("form-beautify-disabled");
        }
        var values = [];
        for(var f =0;f<selectobj.find("option:selected").length;f++)
        {
            var title = selectobj.find("option:selected").eq(f).html();
            title = title.replace(/&nbsp;/g," ");
            values.push(title)
        }
        selectobj.next().find(".form-beautify-text").html(values.join(","));
    };
    var selectClose = function (obj) {
        $("dl",obj).remove();
        obj.removeClass("form-beautify-selected");

    };
    var selectOpen = function (obj) {
        var selectobj = obj.prev("select");
        var is_multiple = selectobj[0].multiple;
        var optionobj = selectobj.find("option,optgroup");
        var is_group = false;
        var dl = $('<dl class="form-beautify-anim form-beautify-anim-upbit" style="" tabindex="-1"></dl>');
        dl.appendTo(obj);
        var is_disabled = selectobj[0].disabled;
        dl.attr("data-multiple",is_multiple);
        dl.empty();
        var is_search = selectobj.attr("data-search");
        if(is_search =="true" && !is_disabled)
        {
            dl.append("<div style='margin:5px 1px;'><input type='text' class='form-beautify-input' tabindex='0'></div>")
            // selecttextobj1.find("input[type='text']").removeAttr("readonly")
        }
        if(is_disabled)
        {
            return false;
        }

        for(var i =0;i<optionobj.length;i++)
        {
            if(optionobj.eq(i)[0].localName == 'optgroup')
            {
                var label = optionobj.eq(i).attr("label");
                var dt = $('<dt>'+label+'</dt>');
                dt.html(label).appendTo(dl);
                is_group = true;
                // dd.appendTo(selecttextobj1.find(".form-beautify-anim"));
            }else{
                var value = optionobj.eq(i).attr("value");
                var html = optionobj.eq(i).html();
                var dd = $('<dd class="" ></dd>');
                dd.attr("data-value",value);
                if(is_multiple)
                {

                    if(optionobj.eq(i)[0].selected)
                    {
                        html = '<input name="ck" form-beautify-skin="primary" title="'+html+'" type="checkbox" value="'+value+'" checked>';
                    }else{
                        html = '<input name="ck" form-beautify-skin="primary" title="'+html+'" type="checkbox" value="'+value+'">';
                    }

                }
                dd.html(html);

                if(optionobj.eq(i).is(":disabled"))
                {
                    dd.addClass("form-beautify-disabled");
                    if(dd.find("input[type='checkbox']").length>0)
                    {
                        dd.find("input[type='checkbox']")[0].disabled = true;
                        dd.find("input[type='checkbox']").trigger("change");
                    }

                }
                dd.appendTo(dl);
            }
        }
        if(is_group)
        {
            obj.find(".form-beautify-anim").addClass("form-beautify-select-group");
        }else{
            obj.find(".form-beautify-anim").removeClass("form-beautify-select-group");
        }
        if(is_multiple)
        {
            $("input[type='checkbox']").formCheckLabel();
        }else{
            var values = selectobj.val();
            obj.find(".form-beautify-anim dd[data-value='"+values+"']").addClass("form-beautify-this");
        }

        $(".form-beautify-select").removeClass("form-beautify-selected");
        obj.addClass("form-beautify-selected");
        if(obj.find("input[type='text']").length>0)
        {
            obj.find("input[type='text']").focus();
        }else{
            obj.find(".form-beautify-anim").focus();
        }
        if(obj.find(".form-beautify-this").size()>0)
        {
            dl.scrollTop(obj.find(".form-beautify-this").eq(0).position().top-45);
        }

    };

    $(function () {
        $("select").formSelect();
        $("input[type='checkbox']").formCheckLabel();
        $("input[type='radio']").formCheckLabel();
        $("body").delegate("input[type='checkbox'],input[type='radio']","change",function () {
            $(this).formCheckLabel();
        });

        $("body").delegate(".form-beautify-anim[data-multiple='false'] dd","click",function (e) {
            var value = $(this).attr("data-value");
            if(!$(this).hasClass("form-beautify-disabled"))
            {
                $(this).parents(".form-beautify-select").prev("select").val(value).trigger("change");

                selectClose($(this).parents(".form-beautify-select"));
            }
        });
        $("body").delegate(".form-beautify-anim[data-multiple='true'] dd input[name='ck']","change",function (e) {
            var value = [];
            var inputobj = $(this).parents("dl").find("input[name='ck']:checked");
            for(var i = 0;i<inputobj.length;i++)
            {
                value.push(inputobj.eq(i).val());

            }
            if(!$(this).is(":disabled"))
            {
                $(this).parents(".form-beautify-select").prev("select").val(value).trigger("change");
            }
        });
        $("body").delegate("select","change",function (e) {

            selectSetValues($(this));
        });
        $("body").delegate(".form-beautify-select  input[type='text']","keyup",function (e) {
            if(!$(this).parents(".form-beautify-select").hasClass("form-beautify-selected"))
            {
                $(this).parents(".form-beautify-select").addClass("form-beautify-selected");
            }
            var selectobj = $(this).parents(".form-beautify-select").prev("select");

            var is_search = selectobj.attr("data-search");
            if(is_search == "true")
            {
                var optionobj = $(this).parents(".form-beautify-select").prev("select").find("option,optgroup");
                var values = $(this).val();
                values = values.split(",");
                var value = values.pop();
                $(this).parents(".form-beautify-select").find("dd").hide();
                for(var i =0;i<optionobj.length;i++)
                {
                    if(optionobj.eq(i)[0].localName == 'option')
                    {
                        var title = optionobj.eq(i).html();
                        if(title.indexOf(value)!= -1)
                        {
                            $(this).parents(".form-beautify-select").find("dd[data-value='"+optionobj.eq(i).val()+"']").show();
                        }
                    }
                }
            }
        });


        $("body").delegate(".form-beautify-select .form-beautify-select-title","click",function (e) {
            if($(this).parents(".form-beautify-select").hasClass("form-beautify-selected"))
            {
                selectClose($(this).parents(".form-beautify-select"));
            }else{
                $(this).find("input[type='text']").focusin();

                selectOpen($(this).parents(".form-beautify-select"))
            }
            return false;
        });

        $("body").delegate(".form-beautify-select .form-beautify-anim","focusout",function (e) {
            var anim_obj = $(this);
            var input_obj = $(this).parents(".form-beautify-select").find("input[type='text']");
            var obj = $(this).parents(".form-beautify-select");
            setTimeout(function () {
                // console.log(anim_obj.is(":focus"),input_obj.is(":focus"))
                if(!input_obj.is(":focus")&&!anim_obj.is(":focus"))
                {
                    selectClose(obj);
                }else if(input_obj.length<1&&!anim_obj.is(":focus"))
                {
                    selectClose(obj);
                }
            },500)
        });
        $(".bTabs").delegate(".next","click",function () {
            var obj = $(this).parents(".bTabs");
            var w1 = $(".tab-header",obj).width();
            var l1 = $(".tab-header",obj).offset().left+25;
            var l2 = 0;
            $(".nav-tabs>li",obj).each(function () {
                if(l2<1)
                {
                    if($(this).offset().left>=(l1+w1))
                    {
                        l2 = $(this).width()+($(this).offset().left-(l1+w1));
                    }
                }
            });
            $(".nav-tabs",obj).offset({left:$(".nav-tabs",obj).offset().left-l2});
        });
        $(".bTabs ").delegate(".priv","click",function () {
            var obj = $(this).parents(".bTabs");
            var l1 = $(".tab-header",obj).offset().left+25;
            var l2 = 0;
            $(".nav-tabs>li",obj).each(function () {

                if($(this).offset().left<l1)
                {
                    l2 = l1-$(this).offset().left;
                }

            });
            $(".nav-tabs",obj).offset({left:$(".nav-tabs",obj).offset().left+l2});
        });
        $(".bTabs").delegate(" .nav-tabs>li","click",function () {
            var id = $(this).attr("data-id");
            var obj = $(this).parents(".bTabs");
            obj.selectTab({id:id});

        });
        $(".bTabs ").delegate(".nav-tabs>li>a>.close","click",function () {
            var id = $(this).parents("li").attr("data-id");
            $(this).parents(".bTabs").closeTab({id:id});
            return false;

        });

    });


})(jQuery);